//
//  StewardViewController.m
//  VTravel
//
//  Created by lanouhn on 16/8/1.
//  Copyright © 2016年 xiaoYu. All rights reserved.
//

#import "StewardViewController.h"
/** 引入管家界面cell */
#import "StewardTableViewCell.h"
/** 引入轮播图第三方 */
#import <SDCycleScrollView/SDCycleScrollView.h>
/** 引入管家表头视图 */
#import "StewardTableHeaderView.h"
/** 引入管家列表model */
#import "StewardListModel.h"
/** 引入点击cell要跳转到的管家详情界面 */
#import "StewareDetailViewController.h"

@interface StewardViewController ()<UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate>
#pragma mark - 属性
/** 底部表视图 */
@property (strong, nonatomic) IBOutlet UITableView *RootTableView;
/** 数据源数组 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 定义表头视图轮播图属性 */
@property (nonatomic, strong) SDCycleScrollView *sdcycleScrollView;
/** 点击性别选择按钮全部会弹出来的UIView */
@property (nonatomic, strong)UIView *GenderView;
/** 点击全部按钮的次数 */
@property (nonatomic, assign) BOOL ClickAllButtoncount;

@end

@implementation StewardViewController

#pragma mark - 懒加载
// 性别选择视图
- (UIView *)GenderView{
    if (_GenderView == nil) {
        NSArray *arr = @[@"全部", @"男", @"女"];
        self.GenderView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight)];
        self.GenderView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.7];
        for (int i = 0; i < 3; i ++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 40 * i, kScreenWidth, 40);
            button.layer.borderWidth = 0.4;
            button.layer.borderColor = [UIColor darkGrayColor].CGColor;
            [button setTitle:arr[i] forState:(UIControlStateNormal)];
            [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
            [button addTarget:self action:@selector(handleButton:) forControlEvents:(UIControlEventTouchUpInside)];
            button.tag = 300 + i;
            button.backgroundColor = [UIColor whiteColor];
            [self.GenderView addSubview:button];
            [self.view addSubview:self.GenderView];
        }
    }
    return _GenderView;
}
// 数据源数组
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark - 视图加载完成的方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ClickAllButtoncount = YES;
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 网络请求数据
        [self requestData];
    });

    // 配置表头视图(轮播图)
    [self creatHeaderView];
    
    /** 注册管家界面cell */
    [self.RootTableView registerNib:[UINib nibWithNibName:@"StewardTableViewCell" bundle:nil] forCellReuseIdentifier:@"StewardTableViewCell"];
}
#pragma mark - 请求数据
- (void)requestData{
    [NetWorkManager requestForGETWithURL:KSTEWARD_URL paramater:@{@"region_id":city_id, @"limit":@"10", @"offset":@"0"} success:^(id responseObject) {

        NSArray *array = responseObject[@"data"];
        for (NSDictionary *dic in array) {
            StewardListModel *model = [[StewardListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.RootTableView reloadData];
        });
                
    } fail:^(NSError *error) {
        kMyLog(@"%@", error);
    }];
}

#pragma mark - 配置表头的轮播图视图
- (void)creatHeaderView{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    self.sdcycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    self.sdcycleScrollView.delegate = self;
    // 暂时设置本地数据源
    self.sdcycleScrollView.localizationImageNamesGroup = @[[UIImage imageNamed:@"banner1"], [UIImage imageNamed:@"home_assisant_ad@2x"]];
    [headerView addSubview:self.sdcycleScrollView];
    self.RootTableView.tableHeaderView = headerView;
}

#pragma mark - 配置区头视图(三个button)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    StewardTableHeaderView *headerView = [[StewardTableHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    headerView.StewardBlock = ^(NSInteger tag){
        if (tag == 202) {
            if (self.ClickAllButtoncount == YES) {
                [self clickALLGenderSelectionView];
                self.ClickAllButtoncount = NO;
            }else{
                // 返回GenderView 视图
                [UIView animateWithDuration:0.2 animations:^{
                    self.ClickAllButtoncount = YES;
                    self.GenderView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight);
                }];
            }
        }
    };
    return headerView;
}
#pragma mark - 区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
#pragma mark - 点击区头的第三个性别选择按钮(全部按钮)的点击方法
- (void)handleButton:(UIButton *)sender{
    UIButton *button =[self.RootTableView viewWithTag:202];
    switch (sender.tag - 300) {
        case 0:
            kMyLog(@"全部");
            // 获取区头视图然后获取全部按钮 然后修改按钮的标题
            [button setTitle:@"全部" forState:(UIControlStateNormal)];
            break;
        case 1:{
            kMyLog(@"男");
            [button setTitle:@"男" forState:(UIControlStateNormal)];
            break;
        }
        case 2:
            kMyLog(@"女");
            [button setTitle:@"女" forState:(UIControlStateNormal)];
            break;
        default:
            break;
    }
    // 返回GenderView 视图
    [UIView animateWithDuration:0.2 animations:^{
        self.ClickAllButtoncount = YES;
        self.GenderView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight);
    }];
}
#pragma mark - 坐标转换的方法
- (void)clickALLGenderSelectionView{
    // 获取头视图在当前视图的位置
    CGRect rectInTableView = [self.RootTableView rectForHeaderInSection:0];
    // 转化左边系统WietableView的父视图
    CGRect rectInSuperView = [self.RootTableView convertRect:rectInTableView toView:self.view];
    // 判断如果rectInSuperView <= 50的时候,这是tabelHeader在上方悬停不再去相对位置
    if (rectInSuperView.origin.y <= 50) {
        rectInSuperView = CGRectMake(rectInSuperView.origin.x, 50, rectInSuperView.size.width, rectInSuperView.size.height);
    }
    // 弹出GenderView视图
    [UIView animateWithDuration:0.2 animations:^{
        self.ClickAllButtoncount = YES;
        self.GenderView.frame = CGRectMake(0, rectInSuperView.origin.y + 50, kScreenWidth, kScreenHeight - rectInSuperView.origin.y - 50);
    }];
}
#pragma mark - UITableView代理方法
#pragma mark  - 返回分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark - 返回分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
#pragma  mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    StewardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StewardTableViewCell" forIndexPath:indexPath];
    if (self.dataArray != 0) {
        cell.model = self.dataArray[indexPath.row];
    }
    return cell;
}
#pragma mark - 配置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
#pragma mark - 点击cell要走的方法(在这里跳转到管家详情界面)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StewareDetailViewController *stewareDVC = [[StewareDetailViewController alloc] init];
    StewardListModel *model = self.dataArray[indexPath.row];
    stewareDVC.stewareId = model.assistant_id;
    stewareDVC.hidesBottomBarWhenPushed = YES;
    stewareDVC.title = model.name;
    [self.navigationController pushViewController:stewareDVC animated:YES];
}


@end
